
mui.plusReady(function(){
	//设置系统状态栏背景色
	plus.navigator.setStatusBarBackground('#F1394C');
});

var fjNetwork = (function() {
	var API_KEY = 'FD6393C5A693091D';
	var BASE_URL = 'http://www.xinzhongzheng.com/api/';
	var version = '1.08';
	mui.getJSON("manifest.json",null,function(data){
		version = data.version.name;		//获取当前版本号
	});
	//排序
	function compare(propertyName) {
		return function(object1, object2) {
			var value1 = object1[propertyName];
			var value2 = object2[propertyName];
			if(value2 < value1) {
				return 1;
			} else if(value2 > value1) {
				return -1;
			} else {
				return 0;
			}
		}
	}
	var signParam = function(datas) {
		var str = '';
		datas.sort(compare("key")); 
		for(var i = 0; i < datas.length; i++) {
			str += datas[i].key + '=' + datas[i].value;
		}
		var hash = hex_md5(str + API_KEY);
		return hash;
	}
	var setParams = function(datas) {
		var strSign = signParam(datas);
		datas.push({
			key: 'sign',
			value: strSign
		});

		var dataObj = new Object();
		for(var i = 0; i < datas.length; i++) {
			dataObj[datas[i].key] = datas[i].value;
		}
		return dataObj;
	}
	var post = function(url, params, successCallback, errorCallback) {
		var paramData = new Object();
		if(params) {
			paramData = setParams(params);
		} else {
			paramData = {
				'sign': hex_md5(API_KEY)
			};
		}
		mui.ajax(BASE_URL+url, {
			data: paramData,
			dataType: 'json',
			type: 'post',
			headers:{
				"H-Sys-Type": "android",
				"H-App-Type": "client",
				"H-Version": version
			},
			crossDomain: true,
			success: function(data) {
				console.log(JSON.stringify(data));
				if(data.code == 1) {
					//成功
					successCallback && successCallback(data);
				} else {
					//错误
					var message = data.message;
					if(message.length == 0) {
						message = '访问服务器发生错误';
					}
					errorCallback && errorCallback(message);
				}

			},
			error: function(xhr, type, status) {
				errorCallback && errorCallback('似乎没网了？请确认后重试。');
				console.log(type);
			}
		});
	}
	var get = function(url, params, successCallback, errorCallback) {
		var paramData = new Object();
		if(params) {
			paramData = setParams(params);
		} else {
			paramData = {
				'sign': hex_md5(API_KEY)
			};
		}
		mui.ajax(BASE_URL+url, {
			data: paramData,
			dataType: 'json',
			type: 'get',
			headers:{
				"H-Sys-Type": "android",
				"H-App-Type": "client",
				"H-Version": version
			},
			crossDomain: true,
			success: function(data) {
//				console.log(JSON.stringify(data));
				if(data.code == 1) {
					//成功
					successCallback && successCallback(data);
				} else {
					//错误
					var message = data.msg;
					if(message.length == 0) {
						message = '访问服务器发生错误';
					}
					errorCallback && errorCallback(message);
				}
			},
			error: function(xhr, type, status) {
				errorCallback && errorCallback('似乎没网了？请确认后重试。');
				console.log(status);
			}
		});
	}
	return {
		post: post,
		get: get,
		signParam: signParam
	}
})();